package com.htfyun.roobofactory;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.htfyun.roobofactory.factorytest.FactoryPreviousItemFloatWindowService;
import com.htfyun.roobofactory.factorytest.FactoryTestItemsEnum;

/**
 *  full sceen is defined in style,
 *  this BaseActivity is used for hide navigation.
 */
public class BaseActivity extends Activity {

    protected final String TAG = getClass().getSimpleName();

    private Activity mActivity;

    private Handler mAutoPassHandler = new Handler();
    private boolean confirmPass = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivity = this;

        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION  // hide naviagtion,and whow again when touch
//                      | View.SYSTEM_UI_FLAG_IMMERSIVE    // use with SYSTEM_UI_FLAG_HIDE_NAVIGATION.only show when wipe
                // top or bottom of screen.
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY // the same with SYSTEM_UI_FLAG_IMMERSIVE,but hide navigation
                // after a short timeout.
                // implements at frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
//                | View.SYSTEM_UI_FLAG_LOW_PROFILE;
        decorView.setSystemUiVisibility(uiOptions);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    }

    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter filter = new IntentFilter(FactoryPreviousItemFloatWindowService.ACTION_FACTORY_PREVIOUS_CLICKED);
        filter.addAction(FactoryPreviousItemFloatWindowService.ACTION_FACTORY_RETURN_CLICKED);

        registerReceiver(previousClickReceiver, filter);

    }

    @Override
    protected void onPause() {
        super.onPause();

        unregisterReceiver(previousClickReceiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestory this = " + this);
    }

    protected Handler getHandler() {
        return mAutoPassHandler;
    }
    protected Activity getActivity() {
        return mActivity;
    }
    private BroadcastReceiver previousClickReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (action == null) {
                return;
            }

            Log.e(TAG, "mActivity = " + mActivity.getClass());
            Log.e(TAG, "enu = " + FactoryTestItemsEnum.test_lcd.getItemClass());

            if (action.equals(FactoryPreviousItemFloatWindowService.ACTION_FACTORY_PREVIOUS_CLICKED)
                    || action.equals(FactoryPreviousItemFloatWindowService.ACTION_FACTORY_RETURN_CLICKED)) {

                for (FactoryTestItemsEnum itemsEnum : FactoryTestItemsEnum.values()) {
                    if (mActivity.getClass() == itemsEnum.getItemClass()) {
                        mActivity.setResult(RESULT_FIRST_USER);
                        ActivityCompat.finishAfterTransition(getActivity());
                        break;
                    }
                }
            }
        }
    };

    protected void startAutoPass(final int stringResId) {

        if (confirmPass) {
            return;
        }
        confirmPass = true;
        mAutoPassHandler.postDelayed(new Runnable() {
            @Override
            public void run() {

                Toast.makeText(getActivity(), stringResId, Toast.LENGTH_LONG).show();

                setResult(RESULT_OK);
                ActivityCompat.finishAfterTransition(getActivity());


            }
        }, 1500);
    }
}
